package com.qf.industy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.dto.PageDto;
import com.qf.industy.entity.Industry;
import com.qf.industy.mapper.IndustryMapper;
import com.qf.industy.service.IndustryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.vo.PageResult;
import com.qf.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 行业表 服务实现类
 * </p>
 *
 * @author
 * @since 2023-10-30
 */
@Service
public class IndustryServiceImpl extends ServiceImpl<IndustryMapper, Industry> implements IndustryService {

    @Autowired
    private IndustryMapper industryMapper;

    @Override
    public PageResult<Industry> pageInfo(PageDto<Industry> pageDto) {
        //分页信息
        IPage<Industry> page = new Page<>(pageDto.getNow(),pageDto.getSize());

        //条件查询
        QueryWrapper<Industry> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(pageDto.getWhere().getName())) {
            qw.lambda().like(Industry::getName, pageDto.getWhere().getName());
        }

        IPage<Industry> pageInfo = this.baseMapper.selectPage(page, qw);

        //封装分页结果
        PageResult<Industry> pageResult = new PageResult<>(pageInfo.getTotal(),pageInfo.getPages(),pageInfo.getRecords());

        return pageResult;
    }

    @Override
    public Result add(Industry industry) {
        if (!StringUtils.isEmpty(industry)){
            industry.setCreateTime(new Date());
            int insert = industryMapper.insert(industry);
            if(insert == 1){
                return new Result(0, "添加成功！");
            }else{
                return new Result(1, "添加失败！非法数据");
            }
        } else{
            return new Result(1, "添加失败！非法数据");
        }
    }
}
